package com.rhino.xchat.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.rhino.base.extension.HTTP_LOG_TAG
import com.rhino.base.extension.baseFlowRequest
import com.rhino.base.extension.printELog
import com.rhino.base.http.HttpService
import com.rhino.base.http.ResultBean
import com.rhino.base.model.BaseModel
import com.rhino.xchat.http.api.AccountApi
import com.rhino.base.extension.toast
import com.rhino.base.extension.userAccountProvider
import com.rhino.xchat.http.ErrorCode
import com.rhino.xchat.http.request.ReqLogin
import com.rhino.xchat.http.response.ResLoginInfo
import com.rhino.xchat.http.response.ResUserInfo
import kotlinx.coroutines.launch


/**
 * @author LuoLin
 * @since Create on 2025/4/1.
 */
class AccountViewModel : BaseModel() {

    val smsLoginLiveData = MutableLiveData<ResLoginInfo?>()
    val pwdLoginLiveData = MutableLiveData<ResLoginInfo?>()
    val getUserInfoLiveData = MutableLiveData<ResUserInfo?>()

    fun smsLogin(phone: String, code: String) {
        viewModelScope.launch {
            baseFlowRequest(request = {
                HttpService.getRetrofitService(AccountApi::class.java)
                    .smsLogin(ReqLogin(phone, code = code))
            }, statusLiveData = statusLiveData).collect { baseBean ->
                when (baseBean) {
                    is ResultBean.Success -> {
                        if (baseBean.data?.isSuccess() == true) {
                            userAccountProvider.saveLoginInfo(baseBean.data)
                            pwdLoginLiveData.postValue(baseBean.data)
                        } else {
                            baseBean.data?.getMessage()?.toast()
                            pwdLoginLiveData.postValue(null)
                        }
                    }

                    is ResultBean.Error -> {
                        printELog(HTTP_LOG_TAG, baseBean.toString())
                        baseBean.exception.getErrorMessage()?.toast()
                        smsLoginLiveData.postValue(null)
                    }
                }
            }
        }
    }

    fun pwdLogin(phone: String, password: String) {
        viewModelScope.launch {
            baseFlowRequest(request = {
                HttpService.getRetrofitService(AccountApi::class.java)
                    .pwdLogin(ReqLogin(phone, password = ReqLogin.encodePassword(password)))
            }, statusLiveData = statusLiveData).collect { baseBean ->
                when (baseBean) {
                    is ResultBean.Success -> {
                        if (baseBean.data?.isSuccess() == true) {
                            userAccountProvider.saveLoginInfo(baseBean.data)
                            pwdLoginLiveData.postValue(baseBean.data)
                        } else {
                            baseBean.data?.getMessage()?.toast()
                            pwdLoginLiveData.postValue(null)
                        }
                    }

                    is ResultBean.Error -> {
                        printELog(HTTP_LOG_TAG, baseBean.toString())
                        baseBean.exception.getErrorMessage()?.toast()
                        pwdLoginLiveData.postValue(null)
                    }
                }
            }
        }
    }
}